<template>
  <div>
    <TheHeader></TheHeader>
    <div>
        <el-menu
          :default-active="activeIndex2"
          class="el-menu-demo"
          mode="horizontal"
          background-color="#303133"
          text-color="#fff"
          active-text-color="#FFE100"
          style="padding-left: 500px;background-color: #303133;"
          >
           <el-menu-item index="1" style="margin-right: 60px;font-size: 18px;" @click="tiaozhuan1"><i class="el-icon-s-home"></i>首页</el-menu-item>
           <el-menu-item index="2" style="margin-right: 60px;font-size: 18px;"  @click="tiaozhuan2"><i class="el-icon-s-shop" ></i>购物中心</el-menu-item>
           <el-menu-item index="3" style="margin-right: 60px;font-size: 18px;"  @click="tiaozhuan3"><i class="el-icon-s-opportunity"></i>车型查询</el-menu-item>
           <el-menu-item index="4" style="margin-right: 60px;font-size: 18px;"  @click="tiaozhuan4"><i class="el-icon-s-claim"></i>关于我们</a></el-menu-item>
           <el-menu-item index="5" style="margin-right: 60px;font-size: 18px;"  @click="tiaozhuan5"><i class="el-icon-user-solid"></i>个人中心</a></el-menu-item>
           </el-menu>
    </div>

    <!-- 定义卡片 -->
<el-card class="box-card" style="padding: 30px;margin-left: 120px;">
<!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>鹰联邦</el-breadcrumb-item>
      <el-breadcrumb-item>查看更多</el-breadcrumb-item>
    </el-breadcrumb>
      <!-- 定义第一行数据 -->
      <el-row :gutter="20" style="margin-top: 20px;">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getItemList">
            <el-button slot="append" icon="el-icon-search" @click="getItemList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 获取商品列表数据 -->
      <el-table :data="itemList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="title" label="商品标题" width="500px">
        </el-table-column>
        <el-table-column prop="sellPoint" label="卖点信息" width="400px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" label="价格(元)" width="120px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="120px">
          <template slot-scope="scope">
              <p v-if="scope.row.status==1" style="color: #0077AA;">有货</p>
              <p v-else style="color: #EE9900;">预售</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status==1" type="primary" icon="el-icon-shopping-cart-full" size="mini" @click="updateItemBtn(scope.row)" round>加入购物车</el-button>
            <el-button v-else type="warning" icon="el-icon-shopping-cart-full" size="mini" @click="updateItemBtn(scope.row)" round>预售购物车</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>

    <TheFooter></TheFooter>
  </div>
</template>

<script>
import TheFooter from '../the/TheFooter.vue'
import TheHeader from '../the/TheHeader.vue'
export default {
  data () {
    return {
      // 1.定义商品列表信息
      itemList: [],
      // activeIndex2: '1',
      item: [
        {
          name: '1',
          url: require('../../assets/images/1.jpg')
        },
        {
          name: '2',
          url: require('../../assets/images/2.jpg')
        },
        {
          name: '3',
          url: require('../../assets/image_index/banner_1.jpg')
        },
        {
          name: '4',
          url: require('../../assets/image_index/banner_2.jpg')
        }
      ],
      // 2.定义分页对象
      queryItemInfo: {
        query: '', // 定义查询参数
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created () {
    // 1.获取商品列表数据
    this.getItemList()
  },
  components: {
    TheHeader,
    TheFooter
  },
  methods: {
    tiaozhuan1 () {
      this.$router.push('/index')
    },
    tiaozhuan2 () {
      this.$router.push('/shopping')
    },
    tiaozhuan3 () {
      this.$router.push('/car')
    },
    tiaozhuan4 () {
      this.$router.push('/about')
    },
    tiaozhuan5 () {
      this.$router.push('/usernew')
    },
    // 实现商品信息分页查询
    async getItemList () {
      const {
        data: result
      } = await this.$http.get('/yingliangbang/getItemList', {
        params: this.queryItemInfo
      })
      if (result.status !== 200) return this.$message.error('商品列表查询失败')
      this.itemList = result.data.rows
      this.total = result.data.total
    },
    // 通过JS格式化时间
    formatDate (row, column, cellValue, index) {
      const date = new Date(cellValue)
      const year = date.getFullYear()
      const month = (date.getMonth() + 1 + '').padStart(2, '0')
      const day = (date.getDate() + '').padStart(2, '0')
      const HH = (date.getHours() + '').padStart(2, '0')
      const MM = (date.getMinutes() + '').padStart(2, '0')
      const SS = (date.getSeconds() + '').padStart(2, '0')
      return year + '-' + month + '-' + day + ' ' + HH + ':' + MM + ':' + SS
    },
    // 条数变化时 调用
    handleSizeChange (size) {
      this.queryItemInfo.pageSize = size
      this.getItemList()
    },
    // 页码值变化时 调用
    handleCurrentChange (current) {
      this.queryItemInfo.pageNum = current
      this.getItemList()
    },
    async updateStatus (item) {
      const {
        data: result
      } = await this.$http.put('/item/updateItemStatus', {
        id: item.id,
        status: item.status
      })
      if (result.status !== 200) return this.$message.error('更新状态失败')
      this.$message.success('更新状态成功')
    },
    async deleteItemBtn (item) {
      // 消息确认框
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 根据id删除数据
        const {
          data: result
        } = await this.$http.delete('/item/deleteItemById', {
          params: {
            id: item.id
          }
        })
        if (result.status !== 200) return this.$message.error('商品删除失败')
        this.$message.success('商品删除成功')
        // 重新获取商品列表信息
        this.getItemList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 转向到商品新增页面
    toAddItem () {
      this.$router.push('/item/addItem')
    },
    closeUpdateDialog () {
      // 重置表格数据
      this.$refs.updateUserRef.resetFields()
    },

    // 修改商品分类信息
    async updateItemBtn (item) {
      this.updateDialogVisible = true
      const {
        data: result
      } = await this.$http.put('/item/updateItem', item)
      if (result.status !== 200) return this.$message.error('更新商品分类失败')
      this.$message.success('更新商品分类成功')
      this.getItemList()
      this.updateDialogVisible = false
    }
  }
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>
 /* top*/
  .el-header {
    background-color: rgb(43,42,50);
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #FFFFFF;
    padding-left: 1px;

    >div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
        font-size: 25px;
      }
    }
  }

</style>
